<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 查询区域 -->
      <div class="search-box">
        <el-form-item label="品牌名称" prop="brandName">
          <el-input
            v-model="queryParams.brandName"
            placeholder="请输入商品品牌名称查询"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.erp_enable_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </div>
      <!--按钮组-->
      <div class="search-btn">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-form-item>
      </div>
      <!--数据展示区-->
      <div class="data-box">
        <el-table v-loading="loading" :data="beandList">
          <el-table-column type="index" label="序号" align="center" width="80"/>
          <el-table-column label="品牌名称" align="center" prop="brandName"/>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.erp_enable_disable" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:post:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDisable(scope.row)"
                v-hasPermi="['system:post:remove']"
              >{{ scope.row.status === 'Y' ? '禁用' : '启用' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination-box">
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <!--添加/修改弹窗-->
      <div class="dialog-box">
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="商品品牌名称" prop="brandName">
              <el-input v-model="form.brandName" placeholder="请输入商品品牌名称"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">保存</el-button>
            <el-button @click="cancel">取消</el-button>
          </div>
        </el-dialog>
      </div>

    </el-form>
  </div>
</template>

<script>
import {listBrand, updateBrand,addBrand} from "@/api/erp/productbrand";

export default {
  name: "productBrand",
  dicts: ['erp_enable_disable'],
  data() {
    return {

      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 总条数
      total: 0,
      // 商品品牌列表
      beandList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        brandName: [
          {required: true, message: "商品品牌名称不能为空", trigger: "blur"}
        ]
      }
    }
  },
  // 页面加载时执行
  created() {
    // 调用查询方法
    this.getList()
  },
  methods: {
    /** 查询商品品牌列表 */
    getList() {
      // 遮罩层开启
      this.loading = true
      // 调用查询方法
      listBrand(this.queryParams).then(response => {
        this.beandList = response.rows
        this.total = response.total
        // 遮罩层关闭
        this.loading = false
      })
    },
    // 查询方法
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置方法
    resetQuery() {
      // 使用RuoYi提供的方法清空查询数据
      this.resetForm("queryForm")
      // 调用查询方法再次查询
      this.handleQuery()
    },
    // 新增商品品牌弹窗方法
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商品品牌"
    },
    // 修改商品品牌弹窗方法
    handleUpdate(row) {
      this.open = true
      this.title = "修改商品品牌"
      this.form = row
    },
    // 启用禁用方法
    handleDisable(row) {
      if (row.status === 'Y') {
        this.$modal.confirm('请确认是否禁用该商品品牌').then(function () {
          row.status = 'N'
          // 调用更新方法
          return updateBrand(row)
        }).catch(() => {})
        console.log("禁用方法")
      } else {
        this.$modal.confirm('请确认是否启用该商品品牌').then(function () {
          row.status = 'Y'
          // 调用更新方法
          return updateBrand(row)
        }).catch(() => {})
        console.log("启用方法")
      }
    },
    // 弹出层保存按钮方法
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid){
          if (this.title === '添加商品品牌') {
            this.form.status = 'Y'
            addBrand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }else {
            updateBrand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 弹出层取消按钮方法
    cancel() {
      // 关闭弹出层
      this.open = false
      // 清空表单数据
      this.reset()

    },
    // 表单重置
    reset() {
      this.form = {
        brandName: undefined
      }
      this.resetForm("form")
    }
  }
}
</script>

<style scoped lang="scss">

</style>
